﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using GG.Business;
using System.IO;

namespace GG.Teacher
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private EditTaskControl editTaskControl;

        private void ShowControl(Control control)
        {
            panel1.Controls.Clear();
            panel1.Controls.Add(control);
            control.Dock = DockStyle.Fill;
        }

        private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.editTaskControl = new EditTaskControl();
            ShowControl(editTaskControl);
        }

        private void saveTaskToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var dialog = new SaveFileDialog();
            dialog.DefaultExt = "task";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                using (var fileStream = new FileStream(dialog.FileName, FileMode.Create))
                {
                    var task = this.editTaskControl.GetTask();

                    XmlSerializer serializer = new XmlSerializer(typeof(Task));
                    serializer.Serialize(fileStream, task);
                }
            }
        }

        private void openTaskToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog();
            dialog.DefaultExt = "task";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                using (var fileStream = new FileStream(dialog.FileName, FileMode.Open))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Task));
                    var task = (Task)serializer.Deserialize(fileStream);
                    if (this.editTaskControl == null)
                    {
                        this.editTaskControl = new EditTaskControl();
                        ShowControl(editTaskControl);
                    }
                    this.editTaskControl.ShowTask(task);
                }
            }
        }

        private void vectorDescentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VektorSpadu.Execute(this.editTaskControl.GetTask());
        }
    }
}
